/**
 * 
 */
package org.mspring.mlog.web.module.web.widget;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.News;
import org.mspring.mlog.entity.NewsCatalog;
import org.mspring.mlog.service.NewsCatalogService;
import org.mspring.mlog.service.NewsService;
import org.mspring.platform.utils.RequestUtils;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 新闻Widget
 * 
 * @author Gao Youbo
 * @since 2013-6-29
 */
@Widget
@RequestMapping("/widget/news")
public class Widget_NewsWidget extends Widget_AbstractWidget {
    @Autowired
    private NewsService newsService;
    @Autowired
    private NewsCatalogService newsCatalogService;

    /**
     * 最新新闻
     * 
     * @param tpl
     *            置顶模板文件
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/recent")
    public String top(@RequestParam(required = false, defaultValue = "/widget/news/recent") String tpl, HttpServletRequest request, HttpServletResponse response, Model model) {
        Integer num = RequestUtils.getRequestParameterAsInteger(request, "num");
        if (num == null) {
            num = 10;
        }
        List<News> newsList = newsService.recent(num);
        model.addAttribute("newsList", newsList);
        return "skin:" + tpl;
    }

    /**
     * 热门新闻
     * 
     * @param tpl
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/hot")
    public String hot(@RequestParam(required = false, defaultValue = "/widget/news/hot") String tpl, HttpServletRequest request, HttpServletResponse response, Model model) {
        Integer num = RequestUtils.getRequestParameterAsInteger(request, "num");
        if (num == null) {
            num = 10;
        }
        Date start = RequestUtils.getRequestParameterAsDate(request, "start");
        Date end = RequestUtils.getRequestParameterAsDate(request, "end");
        List<News> newsList = newsService.hot(num, start, end);
        model.addAttribute("newsList", newsList);
        return "skin:" + tpl;
    }

    /**
     * 新闻分类列表
     * 
     * @param tpl
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/catalog")
    public String catalog(@RequestParam(required = false, defaultValue = "/widget/news/catalog") String tpl, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<NewsCatalog> catalogs = newsCatalogService.getAll();
        model.addAttribute("catalogs", catalogs);
        return "skin:" + tpl;
    }

}
